import json
import logging
import yagmail

import yaml

import os

from jsonpath import jsonpath


def clear_directory():
    # 指定要清空的目录路径
    directory = 'auto/couout'

    # 确认目录存在，如果不存在则创建
    os.makedirs(directory, exist_ok=True)

    for filename in os.listdir(directory):
        file_path = os.path.join(directory, filename)
        if os.path.isfile(file_path):
            os.remove(file_path)


def read_yaml(path):
    """
     读取yaml文件内容
    :param path: 文件路径
    :return: 返回文件全部内容
    示例:  data[""]   data[""][""]
    """
    # 读取YAML文件
    with open(path, 'r') as f:
        data = yaml.safe_load(f)
    # 返回读取到的全部内容
    return data


def read_yaml_json(path, json_path="null"):
    """
    # 读取YAML文件并返回json格式

    Args:
        path:
        json_path:

    Returns:

    """
    with open(path, 'r', encoding="utf-8") as f:
        data = yaml.safe_load(f)
    # 返回读取到的全部内容
    if json_path == "null":
        logging.info(f"读取文件名: [{path}] , 返回全部内容: [{data}]")
        return data

    else:
        result = jsonpath(data, json_path)
        cleaned_result = result[0]
        logging.info(f"读取文件名:[{path}] , 使用jsonpath读取[{json_path}]下的内容: [{cleaned_result}]")
        return cleaned_result


def login_yaml(path):
    with open(path, 'r', encoding="utf-8") as file:
        data = json.load(file)
    return data


def write_yaml(path, data):
    # 写入 YAML 文件
    with open(path, 'w') as f:
        yaml.safe_dump(data, f)


def send_email_with_report(report_path, receivers):
    # 创建一个 yagmail 客户端对象
    yag = yagmail.SMTP(user="782045011@qq.com", password="sdacewpbujxbbdah", host='smtp.qq.com')

    # 邮件主题和正文
    subject = "Allure Report"
    body = "The Allure report is attached."

    # 邮件附件 - 报告的路径
    attachments = [report_path]

    # 发送邮件
    yag.send(
        to=receivers,
        subject=subject,
        contents=body,
        attachments=attachments,
    )

# 新增功能（Add）
# 修改功能（Update）
# 删除功能（Remove）
# Bug 修复（Fix）
# 代码重构（Refactor）
# 代码格式调整（Format）
